package com.levy.annotation;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;
import java.lang.reflect.Method;

public class AnnotationTest1 {

	@Retention(RetentionPolicy.RUNTIME)
	public @interface SimpleAnnotation {
		String value() default "test";
	}

	@Retention(RetentionPolicy.RUNTIME)
	@Inherited
	public @interface InheritedAnnotation {
		String value() default "test";
	}

	// two simple classes that will show that the annotation is *not* inherited
	
	class BaseClass {
		@SimpleAnnotation
		public void test(){}
	}

	class SubclassDoesNotInheritAnnotation extends BaseClass {
	}

	// two simple classes that will show that the annotation *is* inherited
	
	class InheritedBase {
		@InheritedAnnotation
		public void test(){}
	}

	class ChildInheritsAnnotation extends InheritedBase {
	}

	// proof
	@Test
	public void testAnnotations() throws SecurityException, NoSuchMethodException {
		// not inherited
		Annotation[] annotations = BaseClass.class.getAnnotations();
		Method m = BaseClass.class.getMethod("test", null);
		annotations = m.getAnnotations();
		assertEquals(1, annotations.length);
		
		annotations = SubclassDoesNotInheritAnnotation.class.getAnnotations();
		assertEquals(0, annotations.length);

		// inherited
		annotations = InheritedBase.class.getAnnotations();
		m = InheritedBase.class.getMethod("test", null);
		annotations = m.getAnnotations();
		assertEquals(1, annotations.length);
		
		
		annotations = ChildInheritsAnnotation.class.getAnnotations();
		assertEquals(1, annotations.length);
	}

}